/**
 * 
 */
package com.gr.staffpm.widget.dialog.feedback;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.model.StringModel;


/**
 * @author Graham Rhodes 18 Feb 2011 22:43:37
 */
public abstract class TaskRejectPanel extends Panel {

    private static final long serialVersionUID = 1L;
    private final TextArea<String> commentBox;
    private final FeedbackPanel feedbackPanel;
    private final MultiLineLabel title;
    private Task globalTask;

    public TaskRejectPanel(String id, Task task) {
        super(id);

        this.globalTask = task;

        title = new MultiLineLabel("title", "Please leave reason for rejecting task: " + task.getName() + ".");
        title.setOutputMarkupId(true);
        add(title);

        Form<Void> form = new Form<Void>("form");

        feedbackPanel = new FeedbackPanel("feedback", new ContainerFeedbackMessageFilter(this));
        feedbackPanel.setOutputMarkupId(true);
        form.add(feedbackPanel);

        commentBox = new TextArea<String>("commentBox", new Model<String>());
        commentBox.setEscapeModelStrings(true);
        commentBox.setOutputMarkupId(true);
        form.add(commentBox);

        form.add(new AjaxButton("submitButton") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                if (!commentBox.getValue().isEmpty())
                    TaskRejectPanel.this.onSubmit(target, commentBox.getValue(), globalTask);
                else
                    error("Please leave a reason for rejecting the resolution.");
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.addComponent(feedbackPanel);
            };

        });

        form.add(new AjaxButton("cancelButton") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                TaskRejectPanel.this.onCancel(target);
            }

        });
        add(form);

    }

    public abstract void onSubmit(AjaxRequestTarget target, String comment, Task task);

    public abstract void onCancel(AjaxRequestTarget target);

    public void update(AjaxRequestTarget target, Task task) {
        globalTask = task;
        title.setDefaultModel(new StringModel("Please leave reason for rejecting task: " + task.getName() + "."));
        commentBox.setModelValue(new String[] { "" });
        target.addComponent(title);
        target.addComponent(commentBox);
    }
}
